layui.use(["table", "jquery", "layer", "form"], function () {
  var table = layui.table;
  var form = layui.form;
  var layer = layui.layer;
  var $ = layui.jquery;

  table.render({
    elem: "#test-2",
    id: "bottom_table",
    toolbar: "#toolbarDemo-2",
    loading: false,
    height: layui_table_view__height,
    defaultToolbar: ["filter", "print"],
    data:[],
    parseData: function (res) {
      //res 即为原始返回的数据
      // console.log(res);
      return {
        code: 0, //解析接口状态
        msg: "成功", //解析提示文本
        count: res.length, //解析数据长度
        data: res || [], //解析数据列表
      };
    },
    cols: [
      [
        { field: 'id', width: 80, title: 'ID', sort: true, hide: true }
          , { field: 'fileNumber', width: 180, title: '档号', sort: true }
          , { field: 'fondNumber', title: '全宗号', hide: true }
          , { field: 'filesNumber', title: '案卷号', hide: true }
          , { field: 'boxNumber', title: '盒号', hide: true }
          , { field: 'archiveCode', title: '档案馆代码',hide: true  }
          , { field: 'recodeNumber', title: '档案号',hide: true  }
          , { field: 'certNumber', title: '证字号' }
          , { field: 'correctTitle', title: '正题名' }
          , { field: 'maleGender', title: '男性别', hide: true }
          , { field: 'maleName', title: '男姓名', hide: true }
          , { field: 'maleGender', title: '男性别', hide: true }
          , { field: 'maleIdNumber', title: '男身份证', hide: true }
          , { field: 'malebirth', title: '男出生日期', hide: true }
          , { field: 'maleOrigMaritalStatus', title: '男方原婚姻状态', hide: true }
          , { field: 'maleAddrOrWork', title: '男方住址或单位', hide: true }
          , { field: 'maleCertType', title: '男方证件类型', hide: true }
          , { field: 'maleIdType', title: '男方身份类型', hide: true }
          , { field: 'maleNationality', title: '男方国籍', hide: true }
          , { field: 'maleNation', title: '男方民族', hide: true }
          , { field: 'maleLocation', title: '男方常住户口所在地', hide: true }
          , { field: 'maleCertNumber', title: '男方证件印制号', hide: true }
          , { field: 'femaleGender', title: '女性别', hide: true }
          , { field: 'femaleName', title: '女姓名', hide: true }
          , { field: 'femaleIdNumbe', title: '女身份证号', hide: true }
          , { field: 'femalebirth', title: '女出生日期', hide: true }
          , { field: 'femaleOrigMaritalStatus', title: '女方原婚姻状态', hide: true }
          , { field: 'femaleAddrOrWork', title: '女方住址或单位', hide: true }
          , { field: 'femaleCertType', title: '女方证件类型', hide: true }
          , { field: 'femaleIdType', title: '女方身份类型', hide: true }
          , { field: 'femaleNationality', title: '女方国籍', hide: true }
          , { field: 'femaleNation', title: '女方民族', hide: true }
          , { field: 'femaleLocation', title: '女方常住户口所在地', hide: true }
          , { field: 'femaleCertNumber', title: '女方证件印制号', hide: true }
          , { field: 'registAuthority', title: '登记机关', hide: true }
          , { field: 'origRegistAuthority', title: '原登记机关', hide: true }
          , { field: 'registNature', title: '登记性质', hide: true }
          , { field: 'origRegistDate', title: '原登记日期', hide: true }
          , { field: 'underAgency', title: '承办机关', hide: true }
          , { field: 'registName', title: '登记员姓名', hide: true }
          , { field: 'delNot', title: '是否删除', hide: true }
          , { field: 'maritalCertRemark', title: '结婚证备注', hide: true }
          , { field: 'origMaritalRegistDate', title: '原结婚登记日期', hide: true }
          , { field: 'origText', title: '原文标识', hide: true }
          , { field: 'otherRemark', title: '其他备注', hide: true }
          , { field: 'businessType', title: '业务类型', hide: true }
          , { field: 'subjectHeading', title: '主题词', hide: true }
          , { field: 'storageTime', title: '保管期限', hide: true, sort: true }
          , { field: 'note', title: '附件', hide: true }
          , { field: 'year', title: '年度', hide: true }
          , { field: 'categoryNumber', title: '分类', hide: true }
          , { field: 'storageAddress', title: '实体存放地址', hide: true }
          , { field: 'docPage', title: '文件页数', hide: true }
          , { fixed: "right", title: "操作", toolbar: "#barDemo-2", width: 180 },
      ],
    ],
  });

  //头工具栏事件
  table.on("toolbar(test-2)", function (obj) {
    switch (obj.event) {
      case "search_Test_2":
        let tr_arr = $(
          "[lay-id='bottom_table'] .layui-table-body.layui-table-main tr"
        );
        let search_value = $("#test_Reload__input_2").val();
        search_value = search_value.trim();
        // console.log(tr_arr,search_value);
        for (let index = 0; index < tr_arr.length; index++) {
          let text_arr = [];

          let tr_index = tr_arr.eq(index);

          for (let inner = 0; inner < tr_index.children().length; inner++) {
            let td_index = tr_index.children().eq(inner).text();
            td_index = td_index.trim();
            text_arr.push(td_index);
          }

          // console.log(text_arr);
          let have = false;
          for (let index = 0; index < text_arr.length; index++) {
            const text = text_arr[index];

            if (text.indexOf(search_value) > -1) {
              have = true;
            }

            if (index + 1 == text_arr.length && !have) {
              tr_index.css("display", "none");
            } else {
              tr_index.css("display", "");
            }
          }
        }
        break;
      case "showAll_2":
        layer.confirm("是否显示全部？", { icon: 3, title: "提示" }, function (
          index
        ) {
          table.reload(
            "bottom_table",
            {
              page: {
                curr: 1, //重新从第 1 页开始
              },
              url: "/file/dire/fileNumber/" + current_fileNumber,
              where: {},
            },
            "data"
          );
        });
        break;
      case "addNew_2":
        let editIndex = layer.open({
          type: 1,
          title: "添加",
          maxmin: true,
          area: "600px",
          content: TABLE_HTML.bottom_add_tableEditHtml,
        });
        $(document).one("click", "#bottom_add_test_edit__form", function () {
          let d = {};
          let t = $("#popForm-4 [name]").serializeArray();
          $.each(t, function () {
            d[this.name] = this.value;
            if (d[this.name] == "") {
              delete d[this.name];
            }
          });
          d.fileNumber = current_fileNumber;
          d.type = 1;
          // console.log(d);

          if (!d.levelNumber || !d.fileTitle) {
            layer.msg("文件级档案号或文件标题不能为空！");
            return false;
          }

          $.ajax({
            url: "/file",
            type: "POST",
            data: d,
            success: function (rep) {
              console.log(rep);
              if (rep.status == 200) {
                layer.msg("添加成功！");
                table.reload(
                  "bottom_table",
                  {
                    page: {
                      curr: 1, //重新从第 1 页开始
                    },
                    url: "/file/dire/fileNumber/" + current_fileNumber,
                    where: {},
                  },
                  "data"
                );
                layer.close(editIndex);
                return false;
              } else {
                layer.msg("添加失败！");
                console.error(rep);
                return false;
              }
            },
            error: function (error) {
              console.warn(error.statusText);
            },
          });
          return false;
        });
        $(document).one(
          "click",
          "#bottom_add_test_edit__form_init",
          function () {
            layer.confirm("是否重置？", function (index) {
              // form.val("popFromId-3", initData);
              document.getElementById("popForm-4").reset();
              layer.close(index);
            });
          }
        );
        break;
    }
  });

  //监听行工具事件
  table.on("tool(test-2)", function (obj) {
    var data = obj.data;
    switch (obj.event) {
      case "edit-2":
        // console.log(data);
        let initData = {};
        // 编辑框的默认值
        initData = {
          id: data.id,
          fileNumber:data.fileNumber,
          fondNumber: data.fondNumberr,
          filesNumber: data.filesNumber,
          boxNumber: data.boxNumber,
          archiveCode: data.archiveCode,
          recodeNumber: data.recodeNumber,
          certNumber: data.certNumber,
          correctTitle: data.correctTitle,
          maleGender: data.maleGender,
          maleName: data.maleName,
          maleGender: data.maleGender,
          maleIdNumber: data.maleIdNumber,
          malebirth: data.malebirth,
          maleOrigMaritalStatus: data.maleOrigMaritalStatus,
          maleAddrOrWork: data.maleAddrOrWork,
          maleCertType: data.maleCertType,
          maleIdType: data.maleIdType,
          maleNationality: data.maleNationality,
          maleNation: data.maleNation,
          maleLocation: data.maleLocation,
          maleCertNumber: data.maleCertNumber,
          femaleGender: data.femaleGender,
          femaleName: data.femaleName,
          femaleIdNumbe: data.femaleIdNumbe,
          femalebirth: data.femalebirth,
          femaleOrigMaritalStatus: data.femaleOrigMaritalStatus,
          femaleAddrOrWork: data.femaleAddrOrWork,
          femaleCertTypee: data.femaleCertType,
          femaleIdType: data.femaleIdType,
          femaleNationality: data.femaleNationality,
          femaleNation: data.femaleNation,
          femaleLocation: data.femaleLocation,
          femaleCertNumber: data.femaleCertNumber,
          registAuthority: data.registAuthority,
          origRegistAuthority: data.origRegistAuthority,
          registNature: data.registNature,
          origRegistDate: data.origRegistDate,
          underAgency: data.underAgency,
          registName: data.registName,
          delNot: data.delNot,
          maritalCertRemark: data.maritalCertRemark,
          origMaritalRegistDate: data.origMaritalRegistDate,
          origTexte: data.origText,
          otherRemark: data.otherRemark,
          businessType: data.businessType,
          subjectHeading: data.subjectHeading,
          note: data.note,
          year: data.year,
          categoryNumber: data.categoryNumber,
          storageAddress: data.storageAddress,
          docPage: data.docPage,        
        };
        let bottom_editIndex = layer.open({
          type: 1,
          title: "编辑",
          maxmin: true,
          area: "600px",
          content: TABLE_HTML.bottom_tableEditHtml,
          success: function () {
            form.val("popFromId-2", initData);
          },
        });
        $(document).one("click", "#bottom_test_edit__form", function () {
          let d = {};
          let t = $("#popForm-2 [name]").serializeArray();
          $.each(t, function () {
            d[this.name] = this.value;
          });
          // console.log(d);

          if (!d.levelNumber || !d.fileTitle) {
            layer.msg("文件级档案号或文件标题不能为空！");
            return false;
          }
          $.ajax({
            url: "/file",
            type: "PUT",
            data: d,
            success: function (rep) {
              console.log(rep);
              if (rep.status == 200) {
                layer.msg("编辑成功！");
                table.reload(
                  "bottom_table",
                  {
                    page: {
                      curr: 1, //重新从第 1 页开始
                    },
                    url: "/file/dire/fileNumber/" + current_fileNumber,
                    where: {},
                  },
                  "data"
                );
                layer.close(bottom_editIndex);
              } else {
                layer.msg("编辑失败！");
                console.error(rep);
              }
            },
            error: function (error) {
              console.warn(error.statusText);
            },
          });

          return false;
        });
        $(document).one("click", "#bottom_test_edit__form_init", function () {
          layer.confirm("是否重置？", function (index) {
            form.val("popFromId-2", initData);
            layer.close(index);
          });
        });
        break;
      case "del-2":
        layer.confirm("确认删除？", function (index) {
          $.ajax({
            url: "/file/" + data.id,
            type: "DELETE",
            success: function (rep) {
              console.log(rep);
              if (rep.status == 200) {
                layer.msg("删除成功！");

                table.reload(
                  "bottom_table",
                  {
                    url: "/file/dire/fileNumber/" + current_fileNumber,
                    where: {},
                  },
                  "data"
                );
                layer.close(index);
              } else {
                layer.msg("编辑失败！");
                console.error(rep);
              }
            },
            error: function (error) {
              console.warn(error.statusText);
            },
          });
          layer.close(index);
        });
        break;
      case "view-2":
        if (!data.including) {
          layer.msg("无文件");
          return;
        }
        var url = data.filePath.trim();
        var host = location.host;

        layer.open({
          type: 1,
          title: data.levelNumber,
          maxmin: true,
          area: ["50%", "100%"],
          content: `<iframe src="/web/viewer.html?file=http://${host}${url}"frameborder="0" style="width:100%;height:99%;"></iframe>`,
        });
        break;
    }
  });

});
